package com.sky.controller.admin;

import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;


@RestController
@RequestMapping("/admin")
@Slf4j
@Api(tags = "菜品相关接口")
public class DishController {

    @Resource
    private DishService dishService;
    @Resource
    private RedisTemplate redisTemplate;

    @PostMapping("/dish")
    @ApiOperation("新增菜品")
    public Result addDish(@RequestBody DishDTO dishDTO) {

        log.info("进入菜品新增...");
        dishService.addDish(dishDTO);

        //清空缓存数据
        clearCache("dish_" + dishDTO.getCategoryId());
        return Result.success();
    }

    @GetMapping("/dish/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> queryPage(DishPageQueryDTO dishPageQueryDTO) {

        log.info("进入菜品分页查询 {}", dishPageQueryDTO);

        PageResult pageResult = dishService.queryPage(dishPageQueryDTO);

        return Result.success(pageResult);
    }

    @DeleteMapping("/dish")
    @ApiOperation("批量删除菜品")
    public Result delete(@RequestParam List<Long> ids) {

        log.info("批量删除菜品 {}", ids);
        dishService.deleteBatch(ids);

        //清空缓存数据
        clearCache("dish_*");
        return Result.success();
    }

    @GetMapping("/dish/{id}")
    @ApiOperation("根据菜品id返回对应数据")
    public Result<DishVO> getDishById(@PathVariable Long id) {
        return Result.success(dishService.getDishWithFlavorById(id));
    }

    @PutMapping("/dish")
    @ApiOperation("修改菜品")
    public Result<DishVO> updateDish(@RequestBody DishDTO dishDTO) {

        log.info("修改菜品DTO {}", dishDTO);

        dishService.updateDishWithFlavorById(dishDTO);
        //清空缓存数据
        clearCache("dish_*");
        return Result.success();
    }

    @PostMapping("/dish/status/{status}")
    @ApiOperation("菜品的起售停售")
    public Result starOrStopWithDish(@PathVariable("status") Integer status,Long id){
        log.info("菜品的起售停售 {}",status);
        dishService.startOrStop(status,id);

        //将所有菜品缓存数据清理掉,所有dish_开头的key
        clearCache("dish_*");
        return Result.success();
    }

    @GetMapping("/dish/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<DishVO>> list(Long categoryId) {

        //构造redis中的key,规则：dish_分类id
        String key = "dish_" + categoryId;

        //查询redis中是否存在菜品数据
        ValueOperations redis = redisTemplate.opsForValue();
        List<DishVO> list = (List<DishVO>)redis.get(key);
        if(list != null && list.size() > 0){
            //存在,直接返回，无需查询数据库
            return Result.success(list);
        }

        Dish dish = new Dish();
        dish.setCategoryId(categoryId);
        dish.setStatus(StatusConstant.ENABLE);//查询起售中的菜品

        //如果不存再,查询数据库,将查询道德数据放入到redis中
        list = dishService.listWithFlavor(dish);
        redis.set(key,list);

        return Result.success(list);
    }

    /**
     * 清空缓存数据
     * @param pattern
     */
    private void clearCache(String pattern) {
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

}
